package com.dwjm.service.Impl;

import com.dwjm.mapper.StudentMapper;
import com.dwjm.pojo.Student;
import com.dwjm.pojo.StudentVO;
import com.dwjm.service.StudentUserService;
import com.dwjm.utlis.JwtUtil;
import com.dwjm.utlis.PageBean;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;


@Service
public class StudentUserServiceImpl implements StudentUserService {
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public Student getByUserName(String username) {
        Student s = studentMapper.getByUserName(username);
        return s;
    }

    @Override
    public String login(Student s) {
        //创建claims
        Map<String, Object> claims = new HashMap<>();
        claims.put("id", s.getStudentId());
        claims.put("username", s.getUsername());
        //生成jWT令牌
        String token = JwtUtil.genToken(claims);
        return token;
    }

    @Override
    public void register(Student student) {
        //学生ID UUID生成
        student.setStudentId(UUID.randomUUID().toString());
        //学籍状态默认在读
        student.setStatus("正常");
        studentMapper.add(student);
    }

    @Override
    public PageBean<StudentVO> getUserList(Integer pageNum, Integer pageSize, String username, String address) {
        //创建pagebean对象
        PageBean<StudentVO> pb = new PageBean<>();
        //开启分页查询PageHelper
        PageHelper.startPage(pageNum, pageSize);
        //调用mapper
        List<StudentVO> as =studentMapper.list(username,address);
        //Page中提供了方法，可以获取PageHelper分页查询后，得到的总记录数和当前页数据
        Page<StudentVO> p = (Page<StudentVO>) as;
        //把数据填充到PageBean对象中
        pb.setTotal(p.getTotal());
        pb.setItems(p.getResult());
        return pb;
    }
    @Override
    public void UpdateStatusByStudentId(String studentId,String status) {
        studentMapper.updateStatus(studentId,status);
    }

    @Override
    public void update(Student student) {
        studentMapper.update(student);
    }

    @Override
    public void add(Student student) {
        //学生ID UUID生成
        student.setStudentId(UUID.randomUUID().toString());
        //学籍状态默认在读
        student.setStatus("正常");
        studentMapper.add(student);
    }

}
